package com.xuan.juc.add;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/*
* 指定个数线程执行完毕再执行操作
* */
public class CyclicBarrierTest {
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(7,()->{
            System.out.println("集齐");
        });

        for (int i = 0; i < 7; i++) {
            final int temp = i; // 问题：为什么要final
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"收集第" + temp + "个球");
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }

            }).start();
        }
    }
}
